import dipmod.histogram
import logging
from scipy.misc import imread
from scipy.misc import imsave
import sys
from os.path import splitext, basename, join

if __name__ == '__main__':
    FORMAT = '	[%(levelname)-5s]%(asctime)-8s %(filename)s:%(lineno)d %(message)s'
    DATEFORMAT = '%H:%M:%S'
    logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATEFORMAT)
    imgs = sys.argv[1:]
    for img in imgs:
        name, ext = splitext(join("out", basename(img)))
        logging.debug("Processing " + img)
        img = imread(img, mode='P') / 255.0
        logging.debug(img.shape)
        hist = dipmod.histogram.hist_calculate(img)
        hist_equalized_img = dipmod.histogram.hist_adjust(img, hist)

        hist_equlized = dipmod.histogram.hist_calculate(hist_equalized_img)
        imsave(name + "_original_img.jpg", img)
        imsave(name + "_hist_equalized_img.jpg", hist_equalized_img)
        dipmod.histogram.hist_savetofile(hist, name + "_original_hist.pdf")
        dipmod.histogram.hist_savetofile(hist_equlized, name + "_hist_equalized_hist.pdf")
